/* 
  批量删除功能

使用the-table绑定: @getDataBySelectBox="handleSelectionChange"

*/

export default {
  data () {
    return {
      multipleSelection: []
    }
  },
  methods: {
    handleSelectionChange (val) {
      this.multipleSelection = val;

      // console.log(val, 'val');
      // console.log(this.multipleSelection, 'handleSelectionChange');
    },
    // 批量删除
    delByIds_Pigeon (url, id = "id") {
      if (!url) return;
      if (this.multipleSelection.length === 0)
        return this.$message.error('未选中记录')

      this.$confirm('此操作将永久删除这些记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let ids = []

          this.multipleSelection.forEach(x => ids.push(x[id]))

          ids = ids.join(',')

          const { data: res } = await this.$http.delete(url + ids)

          if (res.statusCode !== 20000) return this.$message.error('删除失败')

          this.$message.success('批量删除成功')
          this.getList()

        })
        .catch(() => {
          this.$message('已取消删除!')
        })

    },
    // 单个删除
    delById_Pigeon (url, id) {
      console.log('delById_Pigeon');
      // url和id都是必须的
      if (!(url || id)) return;
      // console.log(id);
      this.$confirm('此操作将永久删除该条记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 删除请求
        const { data: res } = await this.$http.delete(url + id)

        if (res.statusCode !== 20000) return this.$message.error('删除失败')

        this.$message.success('删除成功')
        this.getList()



      }).catch(() => {
        this.$message('已取消删除!')
      })
    }
  }
}